package com.itheima.health.jobs;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.RedisConst;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.DateUtils;
import com.itheima.health.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * @Author likepei
 * @Date 2020/2/11 10:42
 * @Version v1.0
 * @Description 清除数据工作的任务类
 */
public class ClearJob {

    //依赖注入一个Jedis
    @Autowired
    private JedisPool jedisPool;
    @Reference
    private OrderSettingService orderSettingService;
    //定时清理
    public void deleteOrderSetting (){
        System.out.println("DeleteOrderSetting......");
        System.out.println("orderSettingService = " + orderSettingService);
        try {
            //调用service
            //调用DateUtils获取本月一日
            orderSettingService.DeleteOrderSetting(DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth()));
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("服务器异常");
        }
    }

    /**
     * 清空垃圾图片的方法
     */
    public void clearImageJob(){
        System.out.println("clearImageJob.....");

        Jedis jedis = jedisPool.getResource();
        ////进行差值计算, 差值的结果的集合 集合要清理的图片名称的集合
        //Set<String> imageFileNameSet = jedis.sdiff(RedisConst.SETMEAL_PIC_RESOURCES, RedisConst.SETMEAL_PIC_DB_RESOURCES);

        //删除剩余的还在radis中的图片
        Set<String> imageFileNameSet = jedis.smembers(RedisConst.SETMEAL_PIC_RESOURCES);


        //遍历要清理的图片名称的集合
        for (String imageFileName : imageFileNameSet) {
            //调用七牛云工具类中的清除图片的功能
            QiniuUtils.deleteFile2QiNiu(imageFileName);
            //删除redis中的SETMEAL_PIC_RESOURCES集合中的 图片名称
            jedis.srem(RedisConst.SETMEAL_PIC_RESOURCES, imageFileName);
        }
        jedis.close();
    }
}
